package com.qubuyer.base

import android.content.Context
import android.os.Build
import android.os.StrictMode
import com.blankj.utilcode.util.Utils
import com.google.gson.Gson
import com.mob.MobSDK
import com.qubuyer.constant.AppConstant
import com.qubuyer.constant.NetConstant
import com.qubuyer.core.app.BaseApplication
import com.qubyer.okhttputil.json.JsonUtils
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.scwang.smartrefresh.layout.header.ClassicsHeader
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.tencent.tauth.Tencent
import okhttp3.Interceptor

class AM : BaseApplication() {
    companion object {
        @JvmField
        var mWxApi: IWXAPI? = null
        @JvmField
        var mTencent: Tencent? = null

        init {
            //设置全局的Header构建器
            SmartRefreshLayout.setDefaultRefreshHeaderCreator { context: Context?, layout: RefreshLayout? ->
                ClassicsHeader(context) //;//指定为经典Header，默认是 贝塞尔雷达Header
            }
            //设置全局的Footer构建器
            SmartRefreshLayout.setDefaultRefreshFooterCreator { context: Context?, layout: RefreshLayout? -> ClassicsFooter(context).setDrawableSize(20f) }
        }
    }

    override fun onCreate() {
        super.onCreate()
        Utils.init(this)
        registerToWX()
        registerToQQ()
        MobSDK.init(this)
        //解决android N（>=24）系统以上分享 路径为file://时的 android.os.FileUriExposedException异常
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            val builder = StrictMode.VmPolicy.Builder()
            StrictMode.setVmPolicy(builder.build())
        }
    }

    override fun configBaseUrl(): String {
        return NetConstant.BASE_URL
    }

    override fun interceptors(): List<Interceptor> {
        return listOf(TokenInterceptor(),SignInterceptor())
    }

    override fun networkInterceptors(): List<Interceptor> {
        return emptyList()
    }

    private fun registerToWX() {
        //第二个参数是指你应用在微信开放平台上的AppID
        mWxApi = WXAPIFactory.createWXAPI(this, AppConstant.WECHAT_APP_ID, false)
        // 将该app注册到微信
        mWxApi?.registerApp(AppConstant.WECHAT_APP_ID)
    }

    private fun registerToQQ() {
        mTencent = Tencent.createInstance("1109786854", applicationContext) //将123123123改为自己的AppID
    }

    override fun onLowMemory() {
        super.onLowMemory()
    }

    override fun onTrimMemory(level: Int) {
        super.onTrimMemory(level)
    }

    override fun gson(): Gson? {
        return JsonUtils.getGson()
    }
}